﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="~/MainSys/Manager/Controls/ctManagementArticle.ascx.cs" Inherits="KhoaLuan.MainSys.Manager.Controls.ctManagementArticle" %>
<link href="/Style/StyleCommon.css" rel="stylesheet" />
<link href="/Style/modal.css" rel="stylesheet" />
<script src="/Scripts/dluscript.js"></script>
<script type="text/javascript">
    function CreateToggle(id, checked) {
        debugger;
        var idtoggle = "onoffswitch" + id;
        var span = $('<span class="onoffswitch">');
        var input = $('<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox">');
        input.attr('dataid', id);
        input.attr('id', idtoggle);
        if (checked)
            input.attr("checked", "checked");
        input.click(function () {
            debugger;
            var dataid = $(this).attr('dataid');
            var checked = this.checked;
            CallAjaxAspx("POST", "ActiveBaiViet", "/MainSys/ProcessAJAX/AjaxArticle.aspx",
                            {
                                idbaiviet: parseInt(dataid),
                                active: checked
                            }, function (data) {
                                alertSmallBox("Cập nhật bài viết thành công", "1 giây trước...");
                            }, AjaxLoading(true), AjaxLoading(false));
        });
        var label = $('<label class="onoffswitch-label">');
        label.attr('for', idtoggle);
        var span1 = $('<span class="onoffswitch-inner" data-swchon-text="YES" data-swchoff-text="NO">');
        label.append(span1);
        var span2 = $('<span class="onoffswitch-switch">');
        label.append(span2);
        span.append(input);
        span.append(label);
        return span;
    }

    function LoadListArticles(totalitem, target, listArticles) {
        debugger;
        target.empty();
        listArticles.forEach(function (item) {
            var tr = $("<tr>");
            var td = $("<td>").append(Common.CreateCheckBox(item.id));
            tr.append(td);
            td = $("<td>").text(item.title);
            tr.append(td);
            td = $("<td style='text-align:center'>");
            if (item.notice)
                td.append('<i class="fa fa-lg fa-check-circle" style="color:green"></i>');
            tr.append(td);
            td = $("<td>").text(item.createby);
            tr.append(td);
            td = $("<td>").text(item.createdate);
            tr.append(td);
            td = $("<td>").text(item.editby);
            tr.append(td);
            td = $("<td>").text(item.editdate);
            tr.append(td);
            td = $('<td>').append(CreateToggle(item.id, item.active));
            tr.append(td);
            td = $("<td>");
            var btnedit = CreateButton(item.id, "fa-edit");
            debugger;
            btnedit.click(function () {
                location.href = "/MainSys/Manager/Articles/AddNew.aspx?ArticleID=" + $(this).attr('dataid');
            });
            td.append(btnedit);

            var btndel = CreateButton(item.id, "fa-times");
            btndel.click(function () {
                debugger;
                var id = $(this).attr("dataid");
                var sender = $(this);
                confirm("Xác nhận", "Bạn có muốn xóa bài viết này không", "Xóa", "Hủy bỏ", function () {
                    debugger;
                    CallAjaxAspx("POST", "XoaBai", "/MainSys/ProcessAJAX/AjaxArticle.aspx", { listid: [id] }, function (data) {
                        debugger;
                        var responseDel = JSON.parse(data);
                        if (responseDel.Status) {
                            debugger;
                            alertSmallBox("Xóa bài viết thành công", "1 giây trước...");
                            LoadData(1);
                        }
                        else {
                            alertSmallBox("Xóa bài viết thất bại", "1 giây trước...", "error");
                        }
                    }, AjaxLoading(true), AjaxLoading(false));
                });
            });
            debugger;
            td.append(btndel);
            var btnview = CreateButton(item.id, "fa-eye");
            btnview.attr("OrgID", item.orgid)
            btnview.click(function () {
                window.open('/MainSys/Article.aspx?ArticleID=' + $(this).attr("dataid") + "&OrgID=" + $(this).attr("OrgID"), "_blank");
            });
            td.append(btnview);

            tr.append(td);
            target.append(tr);
        });
    }

    function LoadData(page) {
        debugger;
        var numberinpage = 5;
        var keyword = $("#<%=txtTimKiem.ClientID%>").val();
        var dataagr = JSON.stringify({
            keyword: keyword,
            currentpage: parseInt(page),
            numberinpage: numberinpage
        });
        LoadDataWithPaging($("#tablelistArticles > tbody"), $("#divpaging"), page, "POST", "/MainSys/ProcessAJAX/AjaxArticle.aspx/GetListArticle", dataagr, LoadListArticles, numberinpage, LoadData);
    }


    $(document).ready(function () {
        debugger;
        LoadData(1);
        $("#<%=btnThem.ClientID%>").click(function () {
            location.href = "/MainSys/Manager/Articles/AddNew.aspx";
        });
        $("#btnShow").click(function () {
            $('#<%=txtTimKiem.ClientID%>').val("");
            LoadData(1);
        });
        $("#btnDelete").click(function () {
            var listcheckbox = $('input[checktype="selectarticle"]:checked');
            if (listcheckbox.length == 0) {
                alertbox("Vui lòng chọn bài viết muốn xóa");
                return;
            }
            confirm("Confirm", "Bạn có muốn xóa những bài viết đã chọn", "OK", "Cancel", function () {
                var listid = [];
                for (var i = 0; i < listcheckbox.length; i++) {
                    var dataid = $(listcheckbox[i]).attr("dataid");
                    listid.push(dataid);
                }
                CallAjaxAspx("POST", "XoaBai", "/MainSys/ProcessAJAX/AjaxArticle.aspx", { listid: listid }, function (data) {
                    debugger;
                    var responseDel = JSON.parse(data);
                    if (responseDel.Status) {
                        debugger;
                        alertSmallBox("Xóa bài viết thành công", "1 giây trước...");
                        LoadData(1);
                    }
                    else {
                        alertSmallBox("Xóa bài viết thất bại", "1 giây trước...", "error");
                    }
                }, AjaxLoading(true), AjaxLoading(false));

            });
        });

        $('#ckbCheckAll').click(function () {
            var listcheckbox = $('input[checktype="selectarticle"]');
            for (var i = 0; i < listcheckbox.length; i++) {
                listcheckbox[i].checked = this.checked;
            }
        });

    });
</script>
<section id="widget-grid" class="">
    <div class="row">
        <article class="col-sm-12 col-md-12 col-lg-12">

            <div class="alert alert-info alert-block">
                <h4 class="alert-heading" style="font-weight: bold">QUẢN LÝ BÀI VIẾT CHO :
        <label runat="server" id="lbOrg" style="font-weight: bold; display: inline;"></label>
                </h4>
            </div>

            <div class="jarviswidget jarviswidget-color-blueDark" id="wid-id-1" data-widget-editbutton="false" data-widget-fullscreenbutton="false">
                <header>
                    <h2>QUẢN LÝ BÀI VIẾT</h2>
                    <div class="widget-toolbar">
                        <div class="btn-group">
                            <input class="btn btnthem dropdown-toggle btn-xs btn-success" type="button" id="btnThem" runat="server" style="margin-right: 10px;" value="Add new" />
                            <input type="button" value="Show" class="btn btnthem dropdown-toggle btn-xs btn-success" id="btnShow" />
                        </div>
                    </div>
                </header>

                <div class="widget-body widget-hide-overflow padding">
                    <div class="col-xs-12 col-sm-6">
                        <div id="datatable_tabletools_filter" class="dataTables_filter">
                            <label style="float: left">
                                <span class="input-group-addon"><i class="glyphicon glyphicon-search"></i></span>
                                <input type="search" class="form-control" placeholder="input keyword" id="txtTimKiem" runat="server" />
                                <input type="button" onclick="LoadData(1);" value="Search" class="btn btn-default" style="width: 80px;" />
                                <input type="button" id="btnDelete" value="Delete" class="btn btn-default" style="width: 80px;" />
                            </label>
                        </div>
                    </div>
                    <div class="col-sm-12  smart-form">
                        <table id="tablelistArticles" class="table table-bordered">
                            <thead>
                                <tr>
                                    <th>
                                        <label class="checkbox" style="margin-top: -20px">
                                            <input type="checkbox" name="checkbox" id="ckbCheckAll">
                                            <i></i>
                                        </label>
                                    </th>
                                    <th>Title</th>
                                    <th>Notice</th>
                                    <th style="width: 120px;">Create By</th>
                                    <th style="width: 80px;">Create Date</th>
                                    <th style="width: 120px;">Edit By</th>
                                    <th style="width: 80px;">Edit Date</th>
                                    <th>Active</th>
                                    <th style="width: 90px;">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                        <div id="divpaging"></div>
                    </div>
                </div>
            </div>
        </article>
    </div>
</section>
